﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Text;
using GraphQLSample.Domain.Core;

namespace GraphQLSample.Domain.Services
{
    public class UserEventService
        : IUserEventService
    {
        private readonly ISubject<UserEvent> _subject=new ReplaySubject<UserEvent>();
        public UserEventService()
        {
            Events = new ConcurrentStack<UserEvent>();
        }
        public ConcurrentStack<UserEvent> Events {get;}

        public void AddError(Exception e)
        {
            _subject.OnError(e);
        }

        public UserEvent AddEvent(UserEvent @event)
        {
            Events.Push(@event);
            _subject.OnNext(@event);
            return @event;
        }

        public IObservable<UserEvent> Stream()
        {
            return _subject.AsObservable();
        }
    }
}
